package Andrew;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Vector;

import javax.swing.JTextArea;

/**
 * A simple class that opens a socket, sends a message to the server, and
 * terminates.
 * 
 * @author Graeme Stevenson (graeme.stevenson@ucd.ie)
 */
public class TCPSocketClientremote {

	/**
	 * The server host name.
	 */
	public String my_serverHost;

	/**
	 * The server port.
	 */
	public int my_serverPort;

	public boolean play_mode = false;

	/**
	 * Sets the client up with the server details.
	 * 
	 * @param the_serverHost
	 *            the server host name.
	 * @param the_serverPort
	 *            the server port.
	 */

	public Vector<String> movieList;


	public TCPSocketClientremote(final String the_serverHost,
			final int the_serverPort) {
		my_serverHost = the_serverHost;
		my_serverPort = the_serverPort;
	}

	/**
	 * Creates a connection to the server and sends a message.
	 * 
	 * @param a_message
	 *            the message to send to the server.
	 */
	public void sendMessage(final String a) {
		try {
			// Create a connection to the server.
			final Socket toServer = new Socket(my_serverHost, my_serverPort);
			final ListParser LP = new ListParser();

			final PrintWriter out = new PrintWriter(toServer.getOutputStream(),
					true);

			// Write the message to the socket.
			out.println(a);

			final BufferedReader in = new BufferedReader(new InputStreamReader(
					toServer.getInputStream()));

			if (a.equalsIgnoreCase("list")) {
				movieList = new Vector<String>();
				final String msg = in.readLine();
				movieList = LP.MakeList(msg);
				System.out.println(msg);

			} else if (a.split("->")[0].equalsIgnoreCase("play")) {
				final String msg = in.readLine();
				if (msg.split("->")[0].equalsIgnoreCase("playing")) {
					play_mode = true;
				}
			} else {
				final String msg = in.readLine();
			}

			// out.close();
			// toServer.close();

		} catch (final IOException ioe) {
			ioe.printStackTrace();
		} catch (final SecurityException se) {
			se.printStackTrace();
		}
	}

} // end class

